local ls = require "packages/lvshi/utility"

local duwu = fk.CreateSkill {
  name = "lvshi__duwu",
}

Fk:loadTranslationTable{
  ["lvshi__duwu"] = "黩武",
  [":lvshi__duwu"] = "当你需要使用一张伤害类牌时，你可以失去勇策略并视为使用之；回合结束时，你可弃置两张牌并对一名角色造成1点伤害，然后你获得勇策略。",
  ["#lvshi__duwu-use"] = "黩武：你可失去勇策略并视为使用一张伤害类牌",
  ["#lvshi__duwu-damage"] = "黩武：你可弃置两张牌并对一名角色造成1点伤害",

  ["$lvshi__duwu1"] = "琅琊少年诸葛恪，金戈铁马立战勋。",
  ["$lvshi__duwu2"] = "吾岂能有败绩，必须胜！",
}

duwu:addEffect("viewas", {
  pattern = "^(jink,peach,analeptic,nullification)|.|.|.|.|.",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("bt")
    all_names = table.filter(all_names, function(name)
      return Fk:cloneCard(name).is_damage_card
    end)
    local names = player:getViewAsCardNames(duwu.name, all_names, nil, {})
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data, Card.NoSuit, 0)
    card.skillName = duwu.name
    return card
  end,
  enabled_at_play = function(self, player)
    return ls.getStrategy(player, false) == "courage" or ls.getStrategy(player, false) == "schemecourage"
  end,
  enabled_at_response = function(self, player, response)
    return not response and (ls.getStrategy(player, false) == "courage" or ls.getStrategy(player, false) == "schemecourage")
  end,
  before_use = function(self, player, use)
    ls.loseStrategy(player, "courage")
  end,
})

duwu:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(duwu.name) and target == player and ls.getStrategy(player) ~= "courage"
    and ls.getStrategy(player, false) ~= "schemecourage"
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets, cards, success = room:askToChooseCardsAndPlayers(player, {
      targets = room:getOtherPlayers(player),
      max_num = 1,
      min_num = 1,
      max_card_num = 2,
      min_card_num = 2,
      pattern = ".",
      skill_name = duwu.name,
      prompt = "#lvshi__duwu-damage",
      cancelable = true,
    })
    if success and #cards == 2 and #targets == 1 then
      event:setCostData(self, {players = targets, cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local result = event:getCostData(self)
    if result then
      room:throwCard(result.cards, duwu.name, player, player)
      local target = result.players[1]
      room:damage({
        from = player,
        to = target,
        damage = 1,
        skillName = duwu.name,
      })
      ls.gainStrategy(player, "courage")
    end
  end,
})

return duwu